<template>
    <div>
        <div class="room-list-div-top"></div>
        <div class="room-list-div">
            <el-container style="height: 500px;">
                <el-container>
                    <el-header>
                        <el-row>
                            <el-col :span="4" style="text-align: left; font-size: 12px; ">
                                <el-select v-model="query.room_region" @change="playSound('click_one')"  @click.native="playSound('click_one')" placeholder="请选择">
                                    <el-option label="所有夸区" value="" />
                                    <el-option label="跨一" value="跨一" />
                                    <el-option label="跨二" value="跨二" />
                                    <el-option label="跨三A" value="跨三A" />
                                    <el-option label="跨三B" value="跨三B" />
                                    <el-option label="跨四" value="跨四" />
                                    <el-option label="跨五" value="跨五" />
                                    <el-option label="跨六" value="跨六" />
                                    <el-option label="跨七" value="跨七" />
                                    <el-option label="跨八" value="跨八" />
                                </el-select>
                            </el-col>
                            <el-col :span="12" style="text-align: left; font-size: 12px; margin-left: 10px;margin-top: 1px;">
                                <el-input v-model="query.room_title" @focus="playSound('click_one')" placeholder="请输入内容" style="width: 80%;" class="input-with-select">
                                    <el-button slot="append" icon="el-icon-search" @click="get_room_list()" :loading="loadingList"></el-button>
                                </el-input>
                            </el-col>
                            <el-col :span="7" style="text-align: right; font-size: 12px;margin-left: 20px;">
                                <el-button type="primary" icon="el-icon-refresh-right" circle @click="get_room_list()" :loading="loadingList"></el-button>
                            </el-col>
                        </el-row>
                    </el-header>

                    <el-main class="el-mian-config">

<el-table :data="list" @click.native="playSound('click_one')" border :show-header="false" height="340" v-loading="loadingList" element-loading-text="正在链接服务器...">
    <el-table-column fixed width="50px" align="center">
        <template slot-scope="scope">
            <div v-if="scope.row.is_password == 1">
                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-lock"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
            </div>
            <div v-if="scope.row.is_password != 1">
                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-radio"><circle cx="12" cy="12" r="2"></circle><path d="M16.24 7.76a6 6 0 0 1 0 8.49m-8.48-.01a6 6 0 0 1 0-8.49m11.31-2.82a10 10 0 0 1 0 14.14m-14.14 0a10 10 0 0 1 0-14.14"></path></svg>
            </div>
        </template>
    </el-table-column>
    <el-table-column fixed width="80px" prop="room_region" align="left"></el-table-column>
    <el-table-column align="left">
        <template slot-scope="scope">
            <el-popover placement="top-start" trigger="click" width="320">
                <el-descriptions title="" direction="vertical" :column="2" border>
                    <el-descriptions-item label="标题" :span="2">{{ scope.row.room_title }}</el-descriptions-item>
                    <el-descriptions-item label="类型">
                        <el-tag size="small" v-if="scope.row.is_password == 2">无锁</el-tag>
                        <el-tag size="small" v-else>密码房</el-tag>
                    </el-descriptions-item>
                    <el-descriptions-item label="大区">{{ scope.row.room_region }}</el-descriptions-item>
                    <el-descriptions-item label="奖励" :span="2">{{ scope.row.room_reward }}</el-descriptions-item>
                    <el-descriptions-item label="开团时间" :span="2">{{ scope.row.start_time }}</el-descriptions-item>
                    <el-descriptions-item label="报名" :span="2">
                        <div class="room-list-button">
                            <div class="room-list-button-images-div" @click="participate_game(scope.row)">
                                <img src="@/assets/images/button/26.png" class="room-list-button-images" alt="">
                                <span class="room-list-button-text"> 参 加 比 赛</span>
                            </div>
                        </div> 
                    </el-descriptions-item>
                </el-descriptions>
                <el-link type="primary" slot="reference" @click.native="playSound('click_one')">{{ scope.row.room_title }}</el-link>
            </el-popover>
        </template>
    </el-table-column>
    <el-table-column prop="room_player_num" width="100" align="center">
        <template slot-scope="scope">
            座位：{{ scope.row.room_player_num }} / 12
        </template>
    </el-table-column>
</el-table>

                    </el-main> 

                    <el-footer style="text-align: left; font-size: 12px">
                        <el-row>
                            <el-col :span="12" style="text-align: left; font-size: 12px; margin-top: 1px;">
                                <el-button type="primary" icon="el-icon-circle-plus" @click="closeComponents('dialogConvene', true)" :loading="loadingList">发布赛事</el-button>
                            </el-col>
                            <el-col :span="12" style="text-align: right; font-size: 12px; margin-top: 1px;">
                                <el-button type="primary" v-if="!$store.state.users_status" icon="el-icon-thumb" @click="closeComponents('dialogLogin', true)" :loading="loadingList">立即登录</el-button>
                                <el-button type="warning" v-if="!!$store.state.users_status" icon="el-icon-switch-button" @click="loginOut()" :loading="loadingList">退出登陆</el-button>
                            </el-col>
                        </el-row>
                    </el-footer>
                </el-container>
            </el-container>
        </div>
    
        <div class="room-list-div-bottom"></div>


        
        
        <!-- 登录 -->
        <eeuioLogin :DialogLogin="dialogLogin" @MusicSound="playSound" @close="closeComponents"></eeuioLogin>
        <!-- 注册 -->
        <eeuioRegister :DialogRegister="dialogRegister" @MusicSound="playSound" @close="closeComponents"></eeuioRegister>
        <!-- 发布悬赏令 -->
        <conveneFrom :DialogConvene="dialogConvene" @MusicSound="playSound" @close="closeComponents"></conveneFrom>
        
    </div>
</template>
<script>
import eeuioLogin from '@/components/common/assembly/operate/login.vue';
import eeuioRegister from '@/components/common/assembly/operate/register.vue';
import conveneFrom from '@/components/common/assembly/operate/convene.vue';
export default {
    name: 'dashboard',
    data() {
        return {
            loadingList: false,
            dialogConvene: false,
            dialogLogin: false,
            dialogRegister: false,

            query:{
                room_title: '',
                room_region: '',
                valid_operate: 'list',
            },

            list: [],
            listCount: 0,
        };
    },
    created() {
        // this.$store.commit('increment', {asd:123});
        // console.log(this.$store.state.users_status)
        this.get_room_list();
    },
    methods: {
        get_room_list() {
            // 播放音频
            this.playSound('click_one');
            // 加载
            this.loadingList = true;
            this.$post('/api/rooms/list.html', this.query).then(res => {
            	if (res.code == 200) {
					let common_table_info = [];
					for(let i in res.data.list){
						common_table_info.push(res.data.list[i]);
					}
					this.list = common_table_info;
					this.listCount = res.data.total;
            	} 
                this.loadingList = false;
            }).catch(err => {
                console.log(err)
                this.loadingList = false;
            });
        },
        participate_game(items){
            if (!this.$store.state.users_status) {
                this.closeComponents('dialogLogin', true);
                return false;
            }

            // click sound
            this.playSound('click_two');
            // copy
            if (items.is_password == 1) {
                this.$prompt('请输入房间密码', '', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputType: 'password',
                    inputPattern: /^\d{6}$/,
                    inputErrorMessage: '密码格式不正确，请核验后再试！'
                }).then(({ value }) => {
                    this.join_games(items, value);
                }).catch(() => {
                    this.playSound('click_one');
                    return false;
                });
                return false;
            } else {
                this.join_games(items, '');
            }
        },
        join_games(room_info, password) {
            this.playSound('click_two');
            if (!!room_info.room_code) {
                this.$post('/api/rooms-player/create.html', {
                    room_code: room_info.room_code,
                    password: password,
                }).then(res => {
                    if (res.code == 200) {
                        let routers = {path: '/game-room', query: {
                            room_code: room_info.room_code,
                            type: 'join',
                        }};
                        
                        this.$router.push(routers);
                    } else {
                        this.$notify.error({ title: '', message: res.msg});
                    }
                }).catch(err => {
                    console.log(err);
                });
            }
        },
        loginOut() {
            this.$confirm('确定要退出登录吗?', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 删除token
                this.$localClear();
                // 重置 $store
                this.$store.commit('changeUsersInfo', {});
                this.$store.commit('changeUsersToken', "");
                this.$store.commit('changeStatus', false);
            });
        },
        playSound(keyword) {
            this.$parent.playSound(keyword);
        },
        closeComponents(key, value, operate, params) {
            this.playSound('open');
            
            // 发布赛事需要登录
            if (key == 'dialogConvene' && !!value && !this.$localGet('bakal_player_token')) {
                this.dialogLogin = true;
                return false;
            }

            // 在登陆页面点击了注册
            if (key == 'dialogLogin' && !value && operate == 'register') {
                this.dialogLogin = false;
                this.dialogRegister = true;
                return false;
            }
            // 登陆成功
            if (key == 'dialogLogin' && !value && operate == 'login') {
                this.dialogLogin = false;
                return false;
            }
            // 在注册页面点击了登陆
            if (key == 'dialogRegister' && !value && operate == 'login') {
                this.dialogRegister = false;
                this.dialogLogin = true;
                return false;
            }

            this[key] = value;
            if (!!operate && operate == 'convene' && !!params) {
                this.$confirm('赛事已发布成功，是否进入房间内等待？').then(() => {
                    this.participate_game(params);
                });
            }
        },
    },
    components: { conveneFrom, eeuioRegister, eeuioLogin }
};
</script>
<style scoped>

.room-list-div-top{
    width: 50%;
    height: 20px;
    min-width: 200px;
    margin: auto;
    background-image: url('../../../assets/images/other/bakal/border/4.png');
}

.room-list-div-bottom{
    width: 50%;
    height: 20px;
    min-width: 500px;
    margin: auto;
    background-image: url('../../../assets/images/other/bakal/border/3.png');
}

.room-list-div {
    border: 1px;
    border-radius:10px;
    width: 50%;
    height: 70%;
    min-width: 800px;
    min-height: 500px;
    margin: auto;
    margin-top: 2px;
    background-image: url('../../../assets/images/background/binglong-black.jpeg');
}
.room-list-button {
    height: 100%;
    min-width: 100px;
    min-height: 50px;
    margin: auto;
    margin-top: 10px;
    backdrop-filter: blur(100px);
    /* background-image: url('../../../assets/images/background/binglong-black.jpeg'); */
}
.el-header {
    backdrop-filter: blur(100px);
    /* background-color: #b3c0d1; */
    color: #333;
    line-height: 60px;
}
.el-footer{
    backdrop-filter: blur(100px);
    /* background-color: #b3c0d1; */
    color: #333;
    line-height: 60px;
}
.room-list-button-images-div{
    position: relative;
    width: 130px;
    margin: auto;
    margin-top: 10px;
    padding-top: 10px;
    transition: color 3s ease;
}
.room-list-button-images-div:hover {
    .room-list-button-images{
        content: url("../../../assets/images/button/25.png");
    }
    .room-list-button-text{
        color: #C0C0C0;
    }
}
.room-list-button-text {
    user-select: none;
    z-index: 1;
    position: absolute;
    top: 15px;
    left: 13px;
    font-size: 18px;
    color: #DEB887;
    transition: color 3s ease;
}

</style>
